/**
 * 快速验证程序 - 检验多源数据聚合系统是否正常工作
 * 这是一个简化版本，可以快速验证核心功能
 */
public class QuickTestRunner {
    
    public static void main(String[] args) {
        System.out.println("=== 快速验证多源数据聚合系统 ===\n");
        
        try {
            // 1. 创建基础组件
            UserPreferences userPrefs = new UserPreferences();
            userPrefs.setPreferredUnits("metric");
            
            Location testLocation = new Location("Beijing");
            System.out.println("✓ 创建测试环境完成（测试城市：北京）");
            
            // 2. 创建多源API管理器
            MultiAPIManager apiManager = new MultiAPIManager(userPrefs);
            System.out.println("✓ 多源API管理器创建完成");
            
            // 3. 注册API服务
            WeatherAPIManager openWeatherService = new WeatherAPIManager(userPrefs);
            apiManager.registerAPIService(openWeatherService);
            System.out.println("✓ 注册OpenWeatherMap服务：" + openWeatherService.getDisplayName());
            
            WeatherAPIComService weatherAPIService = new WeatherAPIComService(userPrefs);
            apiManager.registerAPIService(weatherAPIService);
            System.out.println("✓ 注册WeatherAPI.com服务：" + weatherAPIService.getDisplayName());
            
            System.out.println("\n=== 开始功能验证 ===");
            
            // 4. 测试服务状态
            System.out.println("\n1. 检查服务状态：");
            for (WeatherAPIService service : apiManager.getRegisteredServices()) {
                boolean available = service.isServiceAvailable();
                System.out.println("  - " + service.getDisplayName() + ": " + 
                    (available ? "✓ 可用" : "✗ 不可用"));
                
                if (available) {
                    long latency = service.testConnection();
                    if (latency > 0) {
                        System.out.println("    连接延迟: " + latency + "ms");
                    }
                }
            }
            
            // 5. 测试故障切换策略
            System.out.println("\n2. 测试故障切换策略：");
            apiManager.setAggregationStrategy(MultiAPIManager.AggregationStrategy.PRIMARY_FAILOVER);
            
            long startTime = System.currentTimeMillis();
            WeatherData weatherData = apiManager.getCurrentWeather(testLocation);
            long responseTime = System.currentTimeMillis() - startTime;
            
            if (weatherData != null) {
                System.out.println("  ✓ 故障切换策略工作正常");
                System.out.println("    温度: " + weatherData.getTemperature() + "°C");
                System.out.println("    湿度: " + weatherData.getHumidity() + "%");
                System.out.println("    描述: " + weatherData.getDescription());
                System.out.println("    响应时间: " + responseTime + "ms");
            } else {
                System.out.println("  ✗ 故障切换策略失败：无法获取天气数据");
            }
            
            // 6. 测试缓存功能
            System.out.println("\n3. 测试缓存功能：");
            
            // 第一次请求
            long firstStart = System.currentTimeMillis();
            WeatherData firstRequest = apiManager.getCurrentWeather(testLocation);
            long firstTime = System.currentTimeMillis() - firstStart;
            
            // 立即第二次请求（应该从缓存获取）
            long secondStart = System.currentTimeMillis();
            WeatherData secondRequest = apiManager.getCurrentWeather(testLocation);
            long secondTime = System.currentTimeMillis() - secondStart;
            
            if (firstRequest != null && secondRequest != null) {
                System.out.println("  第一次请求响应时间: " + firstTime + "ms");
                System.out.println("  第二次请求响应时间: " + secondTime + "ms");
                
                if (secondTime < firstTime / 2) {
                    System.out.println("  ✓ 缓存机制工作正常（响应时间显著减少）");
                } else {
                    System.out.println("  ? 缓存效果不明显（可能缓存未命中）");
                }
            }
            
            // 7. 测试并行聚合策略
            System.out.println("\n4. 测试并行聚合策略：");
            apiManager.setAggregationStrategy(MultiAPIManager.AggregationStrategy.PARALLEL_AGGREGATE);
            
            long aggregateStart = System.currentTimeMillis();
            WeatherData aggregatedData = apiManager.getCurrentWeather(testLocation);
            long aggregateTime = System.currentTimeMillis() - aggregateStart;
            
            if (aggregatedData != null) {
                System.out.println("  ✓ 并行聚合策略工作正常");
                System.out.println("    温度: " + aggregatedData.getTemperature() + "°C");
                System.out.println("    描述: " + aggregatedData.getDescription());
                System.out.println("    响应时间: " + aggregateTime + "ms");
                
                if (aggregatedData.getDescription().contains("Aggregated")) {
                    System.out.println("    ✓ 检测到多源聚合数据");
                } else {
                    System.out.println("    - 使用单源数据（可能其他源不可用）");
                }
            } else {
                System.out.println("  ✗ 并行聚合策略失败");
            }
            
            // 8. 显示可靠性统计
            System.out.println("\n5. 可靠性统计：");
            WeatherDataCache cache = apiManager.getCache();
            
            for (WeatherAPIService service : apiManager.getRegisteredServices()) {
                String sourceId = service.getSourceId();
                double score = cache.getSourceReliabilityScore(sourceId);
                WeatherDataCache.SourceReliabilityStats stats = cache.getSourceStats(sourceId);
                
                System.out.println("  " + service.getDisplayName() + ":");
                System.out.println("    可靠性分数: " + String.format("%.2f", score));
                
                if (stats != null) {
                    System.out.println("    成功/失败: " + stats.getSuccessCount() + "/" + stats.getFailureCount());
                    System.out.println("    平均延迟: " + String.format("%.0f", stats.getAverageLatency()) + "ms");
                }
            }
            
            // 9. 测试预报功能
            System.out.println("\n6. 测试预报功能：");
            try {
                WeatherData[] forecast = apiManager.getForecast(testLocation, 3);
                if (forecast != null && forecast.length > 0) {
                    System.out.println("  ✓ 预报功能工作正常");
                    System.out.println("    获取到 " + forecast.length + " 天预报");
                    
                    for (int i = 0; i < Math.min(2, forecast.length); i++) {
                        WeatherData day = forecast[i];
                        System.out.println("    第" + (i + 1) + "天: " + day.getTemperature() + "°C, " + day.getDescription());
                    }
                } else {
                    System.out.println("  ✗ 预报功能失败：无法获取预报数据");
                }
            } catch (Exception e) {
                System.out.println("  ✗ 预报功能异常: " + e.getMessage());
            }
            
            // 10. 显示缓存统计
            System.out.println("\n7. 缓存统计信息：");
            System.out.println("  " + cache.getCacheStats());
            
            System.out.println("\n=== 验证完成 ===");
            System.out.println("多源数据聚合系统基本功能验证完成！");
            
            // 清理资源
            apiManager.shutdown();
            
        } catch (Exception e) {
            System.err.println("验证过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
} 